package cn.wolfcode.web.controller;


import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @RequiresPermissions(value = {"systemDictionaryItem:list","字典明细页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo){
        if (qo.getParentId() != null){
            PageInfo<SystemDictionaryItem> pageResult = systemDictionaryItemService.query(qo);
            model.addAttribute("pageResult", pageResult);
        }
        List<SystemDictionary> systemDictionaries = systemDictionaryService.listAll();
        model.addAttribute("systemDictionaries",systemDictionaries);


        return "systemDictionaryItem/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"systemDictionaryItem:delete","字典明细删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            systemDictionaryItemService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"systemDictionaryItem:saveOrUpdate","字典明细新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem){


        if (systemDictionaryItem.getId() != null) {
            systemDictionaryItemService.update(systemDictionaryItem);
        }else {
            //判断字典明细里的序号是否为null
            if (systemDictionaryItem.getSequence() == null){
                //根据字典目录的id获取当前页面的数据                                        查询数据库中序号最大值的一条数据
                SystemDictionaryItem sdi =  systemDictionaryItemService.selectByMaxSequence(systemDictionaryItem.getParentId());
                //将查出数据的序号加一在设置到页面传来的对象中
                systemDictionaryItem.setSequence(sdi.getSequence()+1);
            }
            //执行保存操作
            systemDictionaryItemService.save(systemDictionaryItem);
        }
        return new JsonResult();
    }
}
